﻿using System;
using System.Threading;
using com.fukefu.source;
using System.Text.RegularExpressions;
namespace com.fukefu.spider.baidu
{
    public class BaiduZhidao
    {
        Source source = new Source(new MobileUserAgent().Get());
        Random random = new Random();
        //用户名注册
        public bool Register(string name, string pass, string email)
        {
            source.Get("http://zhidao.baidu.com/html/user_reg.html?oldurl=http://zhidao.baidu.com/");
            string html = source.Get("https://passport.baidu.com/v2/api/?getapi&class=reg&tpl=ik&tangram=false", "http://zhidao.baidu.com/html/user_reg.html?oldurl=http://zhidao.baidu.com/");
            string voken = Regex.Match(html, "reg_token='([^']+)'").Groups[1].Value;
            string codestring = Regex.Match(html, "reg_vcodestring='([^']+)'").Groups[1].Value;
            html = source.Post("https://passport.baidu.com/v2/api/?reg", "charset=GB2312" +
                                                                                 "&index=0" +
                                                                                 "&u=" +
                                                                                 "&staticpage=http%3A%2F%2Fzhidao.baidu.com%2Fhtml%2Fjump.html" +
                                                                                 "&registerType=1" +
                                                                                 "&retu=http%3A%2F%2Fzhidao.baidu.com%2Fhtml%2Fuser_reg_ok.html%3Fikfromuser%3Dindex" +
                                                                                 "&tpl=ik" +
                                                                                 "&codestring=" + codestring +
                                                                                 "&token=" + voken +
                                                                                 "&callback=parent.bdPass.api.reg._postCallback" +
                                                                                 "&email=" + email.Replace("@", "%40") +
                                                                                 "&username=" + MyFunction.EncodeByGb2312(name) +
                                                                                 "&loginpass=" + pass +
                                                                                 "&verifypass=" + pass +
                                                                                 "&verifycode=" + InputBox.ShowInputBox("https://passport.baidu.com/cgi-bin/genimage?" + codestring + "&v=" + MyFunction.GetTimestampNow()), "http://zhidao.baidu.com/html/user_reg.html?oldurl=http://zhidao.baidu.com/");
            html = source.Get(Regex.Match(html, "encodeURI\\('([^']+)'\\)").Groups[1].Value);
            return false;
        }
        //登陆
        public bool Login(string name, string pass)
        {
            string time = MyFunction.GetTimestampNow().ToString();
            string html = source.Get("https://passport.baidu.com/?login&psp_tt=2&tpl=ik&fu=http%3A//zhidao.baidu.com/html/loginok.html%3Ft%3D" + time + "&u=http%3A//zhidao.baidu.com/");
            html = source.Post("https://passport.baidu.com/?login", "tpl_ok=" +
                                                                        "&next_target=" +
                                                                        "&tpl=ik" +
                                                                        "&skip_ok=" +
                                                                        "&aid=" +
                                                                        "&need_pay=" +
                                                                        "&need_coin=" +
                                                                        "&pay_method=" +
                                                                        "&u=http%3A%2F%2Fzhidao.baidu.com%2F" +
                                                                        "&fu=http%3A%2F%2Fzhidao.baidu.com%2Fhtml%2Floginok.html%3Ft%3D" + time +
                                                                        "&return_method=get" +
                                                                        "&more_param=" +
                                                                        "&return_type=" +
                                                                        "&psp_tt=2" +
                                                                        "&password=" + pass +
                                                                        "&safeflg=0" +
                                                                        "&isphone=tpl" +
                                                                        "&username=" + name +
                                                                        "&verifycode=" +
                                                                        "&mem_pass=on", "https://passport.baidu.com/?login&psp_tt=2&tpl=ik&fu=http%3A//zhidao.baidu.com/html/loginok.html%3Ft%3D1332551800974&u=http%3A//zhidao.baidu.com/");
            html = source.Get("http://zhidao.baidu.com/html/loginok.html?t=" + time);
            if (html.IndexOf("登录成功") != -1)
            {
                return true;
            }
            return false;
        }
        public string GetHtml(string url)
        {
            return source.Get(url);
        }
        //投赞成票
        public bool Agree(string qid, string aid)
        {
            if (source.Get("http://zhidao.baidu.com/q?ct=28&lm=1&t=" + MyFunction.GetTimestampNow() + "&").IndexOf("true") != -1)
            {
                string html = source.Post("http://zhidao.baidu.com/submit/ajax/", "cm=100669&qid=" + qid + "&aid=" + aid + "&type=1", "	http://zhidao.baidu.com/question/" + qid + ".html");
                if (html.IndexOf("\"errorNo\" : \"0\"") != -1)
                {
                    return true;
                }
            }
            return false;
        }
        //评论
        public bool Comment(string qid, string aid, string content)
        {
            if (source.Get("http://zhidao.baidu.com/q?ct=28&lm=1&t=" + MyFunction.GetTimestampNow() + "&").IndexOf("true") != -1)
            {
                string html = source.Post("http://zhidao.baidu.com/submit/comment/reply/?encoding=gbk", "qid=" + qid + "&aid=" + aid + "&content=" + MyFunction.EncodeByUtf8(content) + "&cun=&cid=", "http://zhidao.baidu.com/question/" + qid + ".html");
                if (html.IndexOf("\"errorNo\" : \"0\"") != -1)
                {
                    return true;
                }
            }
            return false;
        }
        //发表问题
        public bool PostQuestion(string title)
        {
            string html = source.Post("http://zhidao.baidu.com/submit/ajax?jasmine=1", "cm=100001" +
                                                                                    "&query=" +
                                                                                    "&title=" + MyFunction.EncodeByUtf8(title) +
                                                                                    "&detail=" +
                                                                                    "&cid=" + GetQuestionType(title) +
                                                                                    "&pid=" +
                                                                                    "&wealth=0" +
                                                                                    "&team=" +
                                                                                    "&teamid=" +
                                                                                    "&anoy=0" +
                                                                                    "&fix=" +
                                                                                    "&utdata=" + GetGJ() +
                                                                                    "&fr=" +
                                                                                    "&querytype=" +
                                                                                    "&psquery=" +
                                                                                    "&usnsqflag=", "http://zhidao.baidu.com/new?word=");
            string qid = Regex.Match(html, "\"qid\" : \"([0-9]+)\"").Groups[1].Value;
            if (!qid.Equals(""))
            {
                html = source.Post("http://zhidao.baidu.com/submit/", "cm=100001" +
                                                                    "&qid=" + qid +
                                                                    "&cid=171" +
                                                                    "&check=" +
                                                                    "&image=" +
                                                                    "&title=" + MyFunction.EncodeByGb2312(title), "http://zhidao.baidu.com/new?word=");
                if (html.IndexOf("您的问题我们已经收到！请耐心等待网友的回答") != -1)
                {
                    return true;
                }
            }
            return false;
        }
        //追加问题
        public bool PostQuestionAdded(string qid, string title, string content)
        {
            string html = source.Post("http://zhidao.baidu.com/submit/ajax/", "application/x-www-form-urlencoded; charset=UTF-8", "utdata=" + GetGJ() +
                                                                        "&cm=100002" +
                                                                        "&qid=" + qid +
                                                                        "&path=%2Fquestion%2F" + qid + ".html" +
                                                                        "&supply=" + MyFunction.EncodeByUtf8(content) +
                                                                        "&detail=" +
                                                                        "&title=" + MyFunction.EncodeByUtf8(title) +
                                                                        "&pid=", "http://zhidao.baidu.com/question/" + qid + ".html");
            if (html.IndexOf("\"errorNo\" : \"0\"") != -1)
            {
                return true;
            }
            return false;
        }
        //获取问题分类
        public string GetQuestionType(string word)
        {
            string html = source.Post("http://zhidao.baidu.com/q", "ct=28&lm=0&word=" + MyFunction.EncodeByUtf8(word) + "&co=", "	http://zhidao.baidu.com/new?fix=&ct=17&pn=0&tn=ikask&rn=10&word=&cm=1&lm=394496&cidV=100");
            return Regex.Match(html, "cid:([0-9]+),Title:").Groups[1].Value;
        }
        //获取浏览器鼠标轨迹
        private string GetGJ()
        {
            long t1 = MyFunction.GetTimestampNow();
            Thread.Sleep(random.Next(1100));
            long t2 = MyFunction.GetTimestampNow();
            long t2_1 = MyFunction.GetTimestampNow() % 100;
            //轨迹导弹
            string guiji = "0";
            for (int i = 0; i < 6; i++)
            {
                guiji += "," + random.Next(4);
            }
            guiji += ",1,2,2";
            //坐标导弹
            string zuibiao = (200 + random.Next(600)) + "," + (150 + random.Next(500));
            Thread.Sleep((10 + random.Next(15)) * 1000);
            long t3 = MyFunction.GetTimestampNow();
            string line = zuibiao + "\t" + guiji + "\t" + (t3 - t1) + "\t" + "1360,768";
            //返回结果
            string result = "";
            for (int i = 0; i < line.Length; i++)
            {
                int j = (int)line[i];
                result += (result.Equals("") ? "" : ",") + (j ^ t2_1);
            }
            return result + "," + t2 + "0";
        }
    }
}
